package com.example.boot1.service.impl;

import com.example.boot1.bean.VueOrderTimesBean;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import com.example.boot1.mapper.VueOrderInfoMapper;
import com.example.boot1.bean.VueOrderInfo;
import com.example.boot1.service.VueOrderInfoService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VueOrderInfoServiceImpl implements VueOrderInfoService{

    @Autowired
    private VueOrderInfoMapper vueOrderInfoMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return vueOrderInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(VueOrderInfo record) {
        return vueOrderInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(VueOrderInfo record) {
        return vueOrderInfoMapper.insertSelective(record);
    }

    @Override
    public VueOrderInfo selectByPrimaryKey(Integer id) {
        return vueOrderInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(VueOrderInfo record) {
        return vueOrderInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(VueOrderInfo record) {
        return vueOrderInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<VueOrderTimesBean> findOrderInfoById(Integer id) {
        //由于前端的需求需要java 处理数据
        List<VueOrderInfo> vlist=vueOrderInfoMapper.findOrderInfoById(id);

       Map<String,List<VueOrderInfo>>  mv = new HashMap();
        if (null!= vlist && vlist.size()>0){
            for (int i = 0; i < vlist.size(); i++) {
                String keyStr =String.valueOf(vlist.get(i).getHowManyTimes());

                if (!mv.containsKey(keyStr)){
                    List<VueOrderInfo> groupOrderInfo = new ArrayList<>();
                    groupOrderInfo.add(vlist.get(i));
                    mv.put(keyStr,groupOrderInfo);

                }else{
                    List<VueOrderInfo> containObj = mv.get(keyStr);
                    containObj.add(vlist.get(i));
                    mv.put(keyStr,containObj);
                }
            }
        }

        //遍历map调整数据结构
        List<VueOrderTimesBean> votList = new ArrayList<>();

        for(Map.Entry<String, List<VueOrderInfo>> entry : mv.entrySet()){
            VueOrderTimesBean vot = new VueOrderTimesBean();
            String mapKey =  entry.getKey();

            List<VueOrderInfo> mapValue = entry.getValue();
            System.out.println(mapKey+":"+mapValue);
            vot.setBackuo("备用");
            vot.setGoodList(mapValue);
            votList.add(vot);
        }

        return  votList ;
    }

}
